
package com.jf.cloud.order.listener;

import cn.hutool.core.collection.CollUtil;
import com.jf.cloud.api.distribution.bo.DistributionNotifyOrderAndShopBO;
import com.jf.cloud.common.order.bo.EsOrderItemBO;
import com.jf.cloud.common.order.vo.OrderItemVO;
import com.jf.cloud.common.rocketmq.config.RocketMqConstant;
import com.jf.cloud.common.util.Json;
import com.jf.cloud.order.service.OrderItemService;
import ma.glasnost.orika.MapperFacade;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 分销修改订单项分销金额
 *
 * @author zz
 * @date 2021-08-20 09:10:00
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.DISTRIBUTION_NOTIFY_ORDER_SHOP_TOPIC, consumerGroup = RocketMqConstant.DISTRIBUTION_NOTIFY_ORDER_GROUP)
public class DistributionNotifyOrderConsumer implements RocketMQListener<DistributionNotifyOrderAndShopBO> {

    private static final Logger logger = LoggerFactory.getLogger(DistributionNotifyOrderConsumer.class);

    @Autowired
    private OrderItemService orderItemService;
    @Autowired
    private MapperFacade mapperFacade;

    @Override
    public void onMessage(DistributionNotifyOrderAndShopBO message) {
        List<OrderItemVO> orderItemList = message.getDistributionOrderItems();
        if (CollUtil.isEmpty(orderItemList)) {
            return;
        }
        List<EsOrderItemBO> esOrderItemList = mapperFacade.mapAsList(orderItemList, EsOrderItemBO.class);
        logger.info("分销金额计算完成后,分销修改订单项分销金额回调开始... message: " + Json.toJsonString(esOrderItemList));
        orderItemService.updateBatchDistributionAmount(esOrderItemList);
    }
}
